package lex.dm;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.ActionMap;
import javax.swing.JFrame;
import javax.swing.Timer;

import org.lex.app.Application;
import org.lex.swing.Swings;

import lex.dm.ui.MainContentPane;
import lex.dm.ui.MainMenuBar;
import lex.dm.ui.actions.ConnectAction;
import lex.dm.ui.actions.DBMSMetaDataAction;
import lex.dm.utils.Actions;

public class DTree extends Application
{
	public static void main( String[] args )
	{
		Swings.setUseBoldMetalFont( false );
		Application.launch( DTree.class, args );
	}

	public static DTree getApplication()
	{
		return Application.getApplication( DTree.class );
	}

	private JFrame mainframe = null;
	private MainContentPane contentPane = null;
	private Connection connection = null;

	public boolean isConnected()
	{
		return null != this.connection;
	}

	public Connection getConnection()
	{
		return connection;
	}

	public void setConnection( Connection connection )
	{
		this.connection = connection;
		this.setConnected( null != connection );
	}

	private void setConnected( boolean b )
	{
		this.getContentPane().setConnected( b );
		this.getMainMenuBar().setConnected( b );
		ActionMap am = this.getContext().getActionMap();
		am.get( Actions.CONNECT ).setEnabled( !b );
		am.get( Actions.META_DATA ).setEnabled( b );
	}

	@Override
	public String getApplicationID()
	{
		return "dtree";
	}

	@Override
	public Window getRootWindow()
	{
		if( null == mainframe )
		{
			mainframe = new JFrame( "DTree" );
			mainframe.setName( getClass().getName() + ".mainframe" );
			mainframe.setContentPane( this.getContentPane() );
			mainframe.setJMenuBar( this.getMainMenuBar() );
		}
		return mainframe;
	}

	public MainContentPane getContentPane()
	{
		if( null == contentPane )
		{
			contentPane = new MainContentPane();
		}
		return contentPane;
	}

	@Override
	protected void initialize( String[] args )
	{
		this.initializeActions();
		this.getContext().getSessionStorage().restore( this.getRootWindow() );
		this.getRootWindow().setVisible( true );
		// do connect
		final Timer con = new Timer( 300,null );
		con.addActionListener(  new ActionListener(){
			@Override
			public void actionPerformed( ActionEvent e )
			{
				ConnectAction.connect();
				con.stop();
			}
		} );
		con.start();
	}

	@Override
	protected void shutdown()
	{
		if( null != connection )
		{
			try
			{
				connection.commit();
				connection.close();
			}
			catch( SQLException e )
			{
				e.printStackTrace();
			}
		}
		this.getContext().getSessionStorage().save( this.getRootWindow() );
	}
	
	private MainMenuBar mainMenu = null;
	private MainMenuBar getMainMenuBar()
	{
		if( null == mainMenu )
			mainMenu = new MainMenuBar();
		return mainMenu;
	}

	private void initializeActions()
	{
		ActionMap am = this.getContext().getActionMap();
		am.put( Actions.CONNECT, new ConnectAction() );
		am.put( Actions.META_DATA, new DBMSMetaDataAction() );
	}
}
